﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jtSprite
{
    public partial class FormMain : Form
    {
        public List<Sprite> spriteList = new List<Sprite>();

        public FormMain()
        {
            InitializeComponent();
            //initCoordinate();
            //pictureBoxModule_Init();
        }

        private void newSprite()
        {

        }

        private void openSprite()
        {
            openFileDialog.ShowDialog();
        }

        private void saveSprite()
        {
            saveFileDialog.ShowDialog();
        }

        private void exportSprite()
        {
            exportFileDialog.ShowDialog();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openSprite();
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveSprite();
        }

        private void binarySpriteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exportSprite();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            openSprite();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            saveSprite();
        }

        private void exportToolStripButton_Click(object sender, EventArgs e)
        {
            exportSprite();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            newSprite();
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            newSprite();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            initCoordinate();
        }

        private void FormMain_SizeChanged(object sender, EventArgs e)
        {
            //initCoordinate();//re-init coordinate
        }
        

        private void buttonInsertModule_Click(object sender, EventArgs e)
        {
            
        }

        private void listSprites()
        {
            dataGridViewModules.Rows.Clear();
            for (int i = 0; i < Sprite.listSprites.Count; i++)
            {
                Sprite tempSprite = Sprite.listSprites[i];
                dataGridViewModules.Rows.Add(i, tempSprite.ID, tempSprite.X, tempSprite.Y, tempSprite.Width, tempSprite.Height, tempSprite.Description);
            }
        }

        private void buttonNewModule_Click(object sender, EventArgs e)
        {
            Sprite.Add();
            listSprites();
            
        }

        private void buttonLoadModuleImage_Click(object sender, EventArgs e)
        {
            //openFileDialogImage.ShowDialog();
            if (openFileDialogImage.ShowDialog() == DialogResult.OK)
            {
                bitmapLoaded = new Bitmap(openFileDialogImage.FileName);
                //loadColorImage(bitmapLoaded);
                bitmapView = bitmapLoaded;
            }
        }

        //for image loaded
        int scanLengthImageLoaded = 0;
        Color[] colorImageLoaded;
        private void loadColorImage(Bitmap img)// not use any more
        {
            if (img != null)
            {
                int imglen = img.Width*img.Height;
                colorImageLoaded = new Color[imglen];
                scanLengthImageLoaded = img.Width;
                for (int i = 0; i < imglen; i++)
                {
                    colorImageLoaded[i] = img.GetPixel(i % scanLengthImageLoaded, i / scanLengthImageLoaded);
                }
            }
        }

        Bitmap [][]bitmapZoomed;
        int bitmapZoomed_Piece_W = 0;
        int bitmapZoomed_Piece_H = 0;
        private void initZoomBitmap(Bitmap bitmapOriginal, int zoomvalue)
        {
            bitmapZoomed = new Bitmap[zoomvalue][];
            int original_W = bitmapOriginal.Width;
            int original_H = bitmapOriginal.Height;

            bitmapZoomed_Piece_W = original_W / zoomvalue;
            bitmapZoomed_Piece_H = original_H / zoomvalue;
            if (original_W % zoomvalue != 0)
            {
                bitmapZoomed_Piece_W += 1;
            }
            if (original_H % zoomvalue != 0)
            {
                bitmapZoomed_Piece_H += 1;
            }

            for (int i = 0; i < m_ZoomIn; i++)
            {
                bitmapZoomed[i] = new Bitmap[zoomvalue];
                for (int j = 0; j < m_ZoomIn; j++)
                {
                    bitmapZoomed[i][j] = new Bitmap(original_W, original_H);                    
                }
            }
            
        }        
        
        private void updateZoomBitmap(Bitmap original, int piceW, int piceH, int zoomvalue)
        {
            Graphics g_zoom;
            int originalW = original.Width;
            int originalH = original.Height;
            for (int i = 0; i < m_ZoomIn; i++)
            {
                for (int j = 0; j < m_ZoomIn; j++)
                {
                    g_zoom = Graphics.FromImage(bitmapZoomed[i][j]);
                    g_zoom.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                    g_zoom.ScaleTransform(zoomvalue,zoomvalue);
                    g_zoom.DrawImage(original, i * piceW*-1, j * piceH*-1);
                }
            }
        }

        private Bitmap[][] zoomBitmap(Bitmap bitmapOriginal, int zoomvalue)
        {
            initZoomBitmap(bitmapOriginal, zoomvalue);
            updateZoomBitmap(bitmapOriginal, bitmapZoomed_Piece_W, bitmapZoomed_Piece_H, zoomvalue);
            return bitmapZoomed;
        }

        private void drawZoomBitmap(Graphics g, Bitmap [][] bitmapZoomed)
        {
            int originalW = bitmapLoaded.Width;
            int originalH = bitmapLoaded.Height;
            if (bitmapZoomed != null)
            {
                for (int i = 0; i < m_ZoomIn; i++)
                {
                    for (int j = 0; j < m_ZoomIn; j++)
                    {
                        g.DrawImage(bitmapZoomed[i][j], _coordinateX + (i * originalW), _coordinateY + (j * originalH));
                    }
                }
                
            }
            else if(bitmapLoaded != null)
            {
                g.DrawImage(bitmapLoaded, _coordinateX, _coordinateY);
            }
        }

        private void dragBitmap(MouseEventArgs e)
        {
            if (current_MouseState == MOUSE_STATE_DRAGING)//drag pic
            {
                int dragOffsetX = (e.X - mouseDragPrevPosX) / m_ZoomIn;
                int dragOffsetY = (e.Y - mouseDragPrevPosY) / m_ZoomIn;
                setCoordinate(_coordinateX + dragOffsetX, _coordinateY + dragOffsetY);
                mouseDragPrevPosX = e.X;
                mouseDragPrevPosY = e.Y;
            }
        }
        /*
        private Bitmap scaleImage(Bitmap img, Rectangle rect, int scalePercent)
        {
            Bitmap imgScale = new Bitmap(rect.Width* scalePercent, rect.Height*scalePercent);
            Graphics g_Scale = Graphics.FromImage(imgScale);
            g_Scale.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            g_Scale.ScaleTransform(scalePercent, scalePercent);

            Bitmap imgClip = new Bitmap(rect.Width, rect.Height);
            Graphics g_Clip = Graphics.FromImage(imgClip);
            g_Clip.DrawImage(img, rect.X, rect.Y);

            g_Scale.DrawImage(imgClip, 0, 0);

            return imgScale;
        }*/

        private Rectangle getRegionZoomImage(Bitmap img, int scalePercent)
        {
            Rectangle rect = new Rectangle();
            if (img == null)
            {
                rect.Width = (rect.Width + 1 / scalePercent) + 1;
                rect.Height = (rect.Height + 1 / scalePercent) + 1;
                return rect;
            }
            int picBoxW = pictureBoxModule.Width;
            int picBoxH = pictureBoxModule.Height;

            int imgW = img.Width;
            int imgH = img.Height;

            int posX = _coordinateX;
            int posY = _coordinateY;

            

            if (posX >= picBoxW || posY >= picBoxH)
            {
                rect.X = 0;
                rect.Width = 0;
                rect.Y = 0;
                rect.Height = 0;
                rect.Width = (rect.Width / scalePercent) + 1;
                rect.Height = (rect.Height / scalePercent) + 1;
                return rect;
            }
            else
            {
                if (posX < 0)
                {
                    rect.X = posX;
                    int tmpw = imgW + posX;
                    if (tmpw < picBoxW)
                    {
                        if (tmpw >= 0)
                        {
                            rect.Width = tmpw;
                        }
                        else
                        {
                            rect.Width = 0;
                            rect.X = 0;
                        }
                    }
                    else
                    {
                        rect.Width = picBoxW;
                    }
                }
                else
                {
                    rect.X = 0;
                    int tmpw = imgW + posX;
                    if (tmpw >= picBoxW)
                    {
                        rect.Width = picBoxW - posX;
                    }
                    else
                    {
                        rect.Width = imgW;
                    }
                }
                if (posY < 0)
                {
                    rect.Y = posY;
                    int tmph = imgH + posY;
                    if (tmph < picBoxH)
                    {
                        if (tmph >= 0)
                        {
                            rect.Height = tmph;
                        }
                        else
                        {
                            rect.Height = 0;
                            rect.Y = 0;
                        }
                    }
                    else
                    {
                        rect.Height = picBoxH;
                    }
                }
                else
                {
                    rect.Y = 0;
                    int tmph = imgH + posY;
                    if (tmph >= picBoxH)
                    {
                        rect.Height = picBoxH - posY;
                    }
                    else
                    {
                        rect.Height = imgH;
                    }
                }
            }
            //zoom rect
            //rect.X          = rect.X * scalePercent;
            //rect.Y          = rect.Y * scalePercent;
            rect.Width = (rect.Width + 1 / scalePercent) + 1;
            rect.Height = (rect.Height + 1 / scalePercent) + 1;

            return rect;
        }

        Bitmap bitmapView = null;

        private int _coordinateX = 0;
        private int _coordinateY = 0;

        Bitmap bitmapLoaded;

        public void setCoordinate(int x, int y)
        {
            _coordinateX = x ;
            _coordinateY = y ;
        }

        public void initCoordinate()
        {
            _coordinateX = pictureBoxModule.Width >> 1;
            _coordinateY = pictureBoxModule.Height >> 1;
        }

        private void pictureBoxModule_Click(object sender, EventArgs e)
        {
            //for test drawline
            //initCoordinate();
            //pictureBoxModule_Init();
            //pictureBoxModule_DrawGrid();
        }

        public int getRelativePosX(int realPosX)
        {
            return -(_coordinateX - (realPosX / m_ZoomIn));            
        }

        public int getRelativePosY(int realPosY)
        {
            return -(_coordinateY - (realPosY / m_ZoomIn));            
        }

        int mouseDragPrevPosX = 0;
        int mouseDragPrevPosY = 0;
        Rectangle aaa;
        private void pictureBoxModule_MouseMove(object sender, MouseEventArgs e)
        {
            //for test drawline
            aaa = getRegionZoomImage(bitmapLoaded, m_ZoomIn);
            

            labelMousePos.Text = "Pos: " + getRelativePosX(e.X) + "," + getRelativePosY(e.Y) + "cur " + (e.X) + "," + (e.Y) +"  RECT("+aaa.X+","+ aaa.Y+","+aaa.Width+","+aaa.Height+")"+ "\nPos debug: " + _coordinateX + "," + _coordinateY + " state cursor " + current_MouseState;
            //initCoordinate();

            module_updateMouseState(module_CurrentSelected, e.X, e.Y, false, m_ZoomIn);

            if (e.Button == MouseButtons.Left)
            {
                Cursor.Clip = pictureBoxModule.RectangleToScreen(pictureBoxModule.ClientRectangle);//contraint cursor in picture box when drag
                dragBitmap(e);
            }
            else
            {
                Cursor.Clip = Screen.PrimaryScreen.Bounds;//restore windows clip                
            }
            pictureBoxModule.Invalidate();
        }

        private void pictureBoxModule_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;            
        }

        private void pictureBoxModule_MouseEnter(object sender, EventArgs e)
        {
            switch (current_MouseState)
            {
                case MOUSE_STATE_SELECT:
                    this.Cursor = Cursors.Default;                    
                    break;
                case MOUSE_STATE_DRAG:
                    this.Cursor = Cursors.NoMove2D;                    
                    break;
                case MOUSE_STATE_EDIT:
                    break;
            }
        }

        //bool b_isMouseHold = false;
        //int[] mouseState = new int []{
        //0,//select state >> defaul 
        //1,//drag state
        //2,//edit state
        //3,// reservation 
        //};
        const int MOUSE_STATE_SELECT = 0;
        const int MOUSE_STATE_DRAG = 1;
        const int MOUSE_STATE_DRAGING = 2;
        const int MOUSE_STATE_EDIT = 3;
        const int MOUSE_STATE_EDITTING = 4;
        int current_MouseState = 0;

        private void pictureBoxModule_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                switch(current_MouseState)
                {
                    case MOUSE_STATE_SELECT:
                        this.Cursor = Cursors.NoMove2D;
                        current_MouseState = MOUSE_STATE_DRAG;
                        break;
                     case MOUSE_STATE_DRAG:
                     case MOUSE_STATE_DRAGING:
                        this.Cursor = Cursors.Default;
                        current_MouseState = MOUSE_STATE_SELECT;
                        break;
                     case MOUSE_STATE_EDIT:
                    
                        break;                
                }
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                switch (current_MouseState)
                {
                    case MOUSE_STATE_SELECT:
                        //get module contain
                        
                        module_CurrentSelected = module_getModuleID(e.X, e.Y, m_ZoomIn);
                        if (module_CurrentSelected >= 0)
                        {
                            dataGridViewModules.CurrentCell = dataGridViewModules.Rows[module_CurrentSelected].Cells[0];
                        }
                        break;
                    case MOUSE_STATE_DRAG:
                        current_MouseState = MOUSE_STATE_DRAGING;
                        mouseDragPrevPosX = e.X;
                        mouseDragPrevPosY = e.Y;
                        break;
                    case MOUSE_STATE_EDIT:

                        break;
                }
            }
        }

        private void pictureBoxModule_MouseUp(object sender, MouseEventArgs e)
        {
            if (current_MouseState == MOUSE_STATE_DRAGING)
            {
                current_MouseState = MOUSE_STATE_DRAG;
            }
        }

        private void pictureBoxModule_DrawCoordinateAxes(Graphics g, int scalePercent)
        {
            Pen pen = new Pen(Color.Red, scalePercent);
            g.DrawLine(pen, _coordinateX * m_ZoomIn, 0, _coordinateX * m_ZoomIn, pictureBoxModule.Height);
            g.DrawLine(pen, 0, _coordinateY * m_ZoomIn, pictureBoxModule.Width, _coordinateY * m_ZoomIn);           
        }

        private void pictureBoxModule_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            
            if (checkBoxTop.Checked)
            {
                pictureBoxModule_DrawGrid(g, m_ZoomIn);
                pictureBoxModule_DrawCoordinateAxes(g, 1);                
                pictureBoxModule_DrawImageLoaded(g, _coordinateX, _coordinateY, m_ZoomIn);
                
            }
            else
            {
                pictureBoxModule_DrawImageLoaded(g, _coordinateX, _coordinateY, m_ZoomIn);
                pictureBoxModule_DrawGrid(g, m_ZoomIn);
                pictureBoxModule_DrawCoordinateAxes(g, 1);                                                
            }
            module_drawModuleRect(g, module_CurrentSelected, m_ZoomIn);
        }

        const int GRIDWIDTH = 10;
        const int GRIDHEIGHT = 10;
        const int GRIDRULER_INFO_WIDTH = 50;
        const int GRIDRULER_INFO_HEIGHT = 50;
        private void pictureBoxModule_DrawGrid(Graphics g, int scalePercent)
        {            
            int GRID_gap_WIDTH = GRIDWIDTH * scalePercent;
            int GRID_gap_HEIGHT = GRIDHEIGHT * scalePercent;
            if (checkBoxGrid.Checked)
            {
                Pen pen = new Pen(Color.DarkCyan);
                int startX = (_coordinateX * scalePercent) % GRID_gap_WIDTH;
                int startY = (_coordinateY * scalePercent) % GRID_gap_HEIGHT;
                int gridPosX, gridPosY;
                for (int i = startX; i < pictureBoxModule.Width; i += GRID_gap_WIDTH)
                {
                    //g.ScaleTransform(scalePercent, scalePercent);
                    g.DrawLine(pen, i, 0, i, pictureBoxModule.Height);
                    gridPosX = getRelativePosX(i);//text grid pos X
                    //DrawStringArial(g, gridPosX.ToString(), 8, Color.Red, i + 1, 0);
                    if (gridPosX % GRIDRULER_INFO_WIDTH == 0)
                    {
                        pictureBoxModule_DrawGridInfo(g, gridPosX.ToString(), 8, Color.Red, i + 1, 0, scalePercent);
                    }
                }
                for (int j = startY; j < pictureBoxModule.Height; j += GRID_gap_HEIGHT)
                {
                    g.ScaleTransform(1, 1);
                    g.DrawLine(pen, 0, j, pictureBoxModule.Width, j);
                    gridPosY = getRelativePosY(j);//text grid pos Y
                    //DrawStringArial(g, gridPosY.ToString(), 8, Color.Red, 0, j + 1);
                    if (gridPosY % GRIDRULER_INFO_HEIGHT == 0)
                    {
                        pictureBoxModule_DrawGridInfo(g, gridPosY.ToString(), 8, Color.Red, 0, j + 1, scalePercent);
                    }
                }
            }

        }

        private void pictureBoxModule_DrawGridInfo(Graphics g, String s, int fontsize, Color color, int x, int y, int m_ZoomIn)
        {
            DrawStringArial(g, s, 8, Color.Red, x, y);
        }

        private void pictureBoxModule_DrawImageLoaded(Graphics g, int x, int y, int m_ZoomIn)
        {
            if (bitmapView != null)
            {   
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;//for fix bug missing pixel edge after scale
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.ScaleTransform(m_ZoomIn, m_ZoomIn);      
                g.DrawImage(bitmapLoaded, new Point(x , y ));
                g.ResetTransform();
            }
        }

        private void DrawStringArial(Graphics g, String s, int fontsize, Color color, int posX, int posY)
        {
            // Create font and brush.
            Font drawFont = new Font("Arial", fontsize);
            SolidBrush drawBrush = new SolidBrush(color);
            // Create point for upper-left corner of drawing.
            float x = posX;
            float y = posY;
            // Set format of string.
            StringFormat drawFormat = new StringFormat();
            //drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            // Draw string to screen.
            g.DrawString(s, drawFont, drawBrush, x, y, drawFormat);
        }

        private void groupBoxModule_Enter(object sender, EventArgs e)
        {

        }

        private void checkBoxGrid_CheckedChanged(object sender, EventArgs e)
        {            
            pictureBoxModule.Invalidate();
        }

        int m_ZoomIn = 1;
        
        private void scrollZoom(int newValue)
        {            
            hScrollBarZoom.Value = newValue;
            m_ZoomIn = hScrollBarZoom.Value + 1;            
            pictureBoxModule.Invalidate();
            label_zoom.Text = "Zoom: " + m_ZoomIn + "00%";
        }

        private void hScrollBarZoom_Scroll(object sender, ScrollEventArgs e)
        {
            scrollZoom(e.NewValue);            
        }

        private void buttonBottom100Percent_Click(object sender, EventArgs e)
        {
            hScrollBarZoom.Value = 0;
            m_ZoomIn = 1;
            label_zoom.Text = "Zoom: " + m_ZoomIn + "00%";
            pictureBoxModule.Invalidate();
        }

        private void checkBoxTop_CheckedChanged(object sender, EventArgs e)
        {
            pictureBoxModule.Invalidate();
        }

        

        private void dataGridViewModules_SelectionChanged(object sender, EventArgs e)
        {
            module_CurrentSelected = dataGridViewModules.CurrentRow.Index;


            //MessageBox.Show("Index select: " + module_CurrentSelected.ToString(),
                    //"Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        ///////////////////////////////////Draw Module Rect
        int module_CurrentSelected = -1;
        bool module_is_MultiSelected = false;

        private void module_drawMultiModules(Graphics g, int scalePercent)//chit this function was not checked yet!.
        {
            int selectedCount = dataGridViewModules.SelectedRows.Count;
            int moduleID;
            for (int i = 0; i < selectedCount; i++)
            {
                moduleID = dataGridViewModules.SelectedRows[i].Index;
                module_drawModuleRect(g, moduleID, scalePercent);
            }
        }

        private Rectangle module_getModuleRect(int moduleID, int scalePercent)
        {
            Rectangle mod_rect = new Rectangle();
            if (moduleID != -1)
            {
                mod_rect.X = (Sprite.listSprites[moduleID].X + _coordinateX) * scalePercent;
                mod_rect.Y = (Sprite.listSprites[moduleID].Y + _coordinateY) * scalePercent;
                mod_rect.Width = Sprite.listSprites[moduleID].Width * scalePercent;
                mod_rect.Height = Sprite.listSprites[moduleID].Height * scalePercent;
            }
            return mod_rect;
        }

        private void module_drawModuleRect(Graphics g, int moduleID, int scalePercent)
        {
            if (moduleID != -1)
            {
                Pen dotline = new Pen(Color.Red);
                Pen dotline2 = new Pen(Color.White);

                Rectangle mod_rect = module_getModuleRect(moduleID, scalePercent);

                float[] dashValues = {3, 3};
                dotline.DashPattern = dashValues;
                g.DrawRectangle(dotline, mod_rect);

                dotline2.DashOffset = 3;
                dotline2.DashPattern = dashValues;
                g.DrawRectangle(dotline2, mod_rect);
            }
        }

        private int module_getModuleID(int pointerX, int pointerY, int scalePercent)
        {
            Rectangle mod_rect;// = new Rectangle();
            //int x, y, w, h;
            for (int i = 0; i < Sprite.listSprites.Count; i++)
            {
                mod_rect = module_getModuleRect(i, scalePercent);
                if (mod_rect.Contains(pointerX, pointerY))
                {
                    return i;
                }
            }
            return module_CurrentSelected;
            
        }

        private void module_editModule(Graphics g, int moduleID, int pointerX, int pointerY, int scalePercent)
        {

        }

        const int MODULE_CURSOR_DEFAULT = 0;
        const int MODULE_CURSOR_EDIT_WIDTH = 1;
        const int MODULE_CURSOR_EDIT_HEIGHT = 2;
        const int MODULE_CURSOR_EDIT_TOP_LEFT = 3;
        const int MODULE_CURSOR_EDIT_BOT_LEFT = 4;
        const int MODULE_CURSOR_EDIT_TOP_RIGHT = 5;
        const int MODULE_CURSOR_EDIT_BOT_RIGHT = 6;
        const int MODULE_CURSOR_EDIT_MOVE_MODULE = 7;
        const int MODULE_CURSOR_EDIT_MOVE_MULTI_MODULES = 8;

        const int MODULE_CHANGE_CURSOR_MARGIN = 3;

        int module_cursorEditState = MODULE_CURSOR_DEFAULT;

        private void module_updateMouseState(int moduleID, int pointerX, int pointerY, bool multiSelected, int scalePercent)
        {
            Rectangle mod_rect = module_getModuleRect(moduleID, scalePercent);
            if (multiSelected)
            {
                module_cursorEditState = MODULE_CURSOR_EDIT_MOVE_MULTI_MODULES;
                return;
            }
            if (current_MouseState == MOUSE_STATE_SELECT)
            {
                if (mod_rect.Left == pointerX )
                {
                    if (mod_rect.Top == pointerY)
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_TOP_LEFT;
                        //this.Cursor = Cursors.SizeNWSE;//resize left width and top height
                    }
                    else if (mod_rect.Bottom == pointerY)
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_BOT_LEFT;
                        //this.Cursor = Cursors.SizeNESW;//resize left width and bottm height
                    }
                    else
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_WIDTH;
                        //this.Cursor = Cursors.SizeWE;//resize left width
                    }
                }
                else if(mod_rect.Right == pointerX)//resize width
                {
                    if (mod_rect.Top == pointerY)
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_TOP_RIGHT;
                        //this.Cursor = Cursors.SizeNESW;//resize right width and top height
                    }
                    else if (mod_rect.Bottom == pointerY)
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_BOT_RIGHT;
                        //this.Cursor = Cursors.SizeNWSE;//resize right width and bottm height
                    }
                    else
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_WIDTH;
                        //this.Cursor = Cursors.SizeWE; //resize right width
                    }
                   
                }
                else if (mod_rect.Top == pointerY )//resize height
                {
                    module_cursorEditState = MODULE_CURSOR_EDIT_HEIGHT;
                    //this.Cursor = Cursors.SizeNS;
                }
                else if(mod_rect.Bottom == pointerY)//resize height
                {
                    module_cursorEditState = MODULE_CURSOR_EDIT_HEIGHT;
                    //this.Cursor = Cursors.SizeNS;
                }

                else if (Math.Abs(pointerX - mod_rect.Left) > MODULE_CHANGE_CURSOR_MARGIN &&
                    Math.Abs(pointerX - mod_rect.Right) > MODULE_CHANGE_CURSOR_MARGIN &&
                    Math.Abs(pointerY - mod_rect.Top) > MODULE_CHANGE_CURSOR_MARGIN &&
                    Math.Abs(pointerY - mod_rect.Bottom) > MODULE_CHANGE_CURSOR_MARGIN 
                    )
                {
                    if (mod_rect.Contains(pointerX, pointerY))//move module
                    {
                        module_cursorEditState = MODULE_CURSOR_EDIT_MOVE_MODULE;
                        //this.Cursor = Cursors.SizeAll;
                    }
                    else
                    {
                        module_cursorEditState = MODULE_CURSOR_DEFAULT;
                        this.Cursor = Cursors.Default;
                    }
                }
                module_processMouseState(moduleID);
            }
            
        }

        private void module_processMouseState( int moduleID)
        {
            switch (module_cursorEditState)
            {
                //case MODULE_CURSOR_DEFAULT:
                    //this.Cursor = Cursors.Default;
                //break;

                case MODULE_CURSOR_EDIT_WIDTH:
                    this.Cursor = Cursors.SizeWE;//resize left width
                break;

                case MODULE_CURSOR_EDIT_HEIGHT:
                    this.Cursor = Cursors.SizeNS;
                break;

                case MODULE_CURSOR_EDIT_TOP_LEFT:
                    this.Cursor = Cursors.SizeNWSE;//resize left width and top height
                break;

                case MODULE_CURSOR_EDIT_BOT_LEFT:
                    this.Cursor = Cursors.SizeNESW;//resize left width and bottm height
                break;

                case MODULE_CURSOR_EDIT_TOP_RIGHT:
                    this.Cursor = Cursors.SizeNESW;//resize right width and top height
                break;

                case MODULE_CURSOR_EDIT_BOT_RIGHT:
                    this.Cursor = Cursors.SizeNWSE;//resize right width and bottm height
                break;

                case MODULE_CURSOR_EDIT_MOVE_MODULE:
                    this.Cursor = Cursors.SizeAll;
                break;

                case MODULE_CURSOR_EDIT_MOVE_MULTI_MODULES:
                    this.Cursor = Cursors.SizeAll;
                break;

            }
        }

    }
}
